package cn.ecut.dao;

import cn.ecut.model.Content;
import cn.ecut.model.User;
import cn.ecut.util.JDBCUtil;
import cn.ecut.util.TableNames;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContentDao {
    private UserDao userDao = new UserDao();
    private String column = " content, publish_time , like_count, user_id ";

    public boolean addContent(Content c) {
        String SQL = "insert into " + TableNames.CONTENT_TABLE_NAME + "( content,  user_id) values ( ? , ?)";
        return JDBCUtil.executeSQL(SQL, c.getContent(), c.getUser().getId());
    }

    public List<Content> list(Integer begin) {
        String SQL = "select id , " + column + " from " + TableNames.CONTENT_TABLE_NAME + " order by id limit ? , ? ";
        ResultSet resultSet = JDBCUtil.selectSQL(SQL, begin, 15);
        List<Content> list = null;
        try {
            list = warp(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public Content loadById(Integer id) {
        String SQL = "select id , " + column + " from" + TableNames.CONTENT_TABLE_NAME + " where id = ?";
        ResultSet resultSet = JDBCUtil.selectSQL(SQL, id);
        List<Content> warp = null;
        try {
            warp = warp(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (warp.size() > 0) {
            return warp.get(0);
        }
        return null;
    }


    private List<Content> warp(ResultSet resultSet) throws Exception {
        List<Content> list = new ArrayList<>();
        int count=0;
        try {
            while (resultSet.next()) {
                Content c = new Content();
                c.setId(resultSet.getInt("id"));
                c.setContent(resultSet.getString("content"));
                c.setPublishTime(resultSet.getTimestamp("publish_time"));
                c.setLikeCount(resultSet.getInt("like_count"));
                User user = userDao.loadByUserId(resultSet.getInt("user_id"));
                c.setUser(user);
                list.add(c);

                count++;
            }
            System.out.println("resultSet有"+count+"个结果");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public boolean updateLikeCount(Content content){
        String SQL = "update"+TableNames.CONTENT_TABLE_NAME+"set like_count = ? where id = ?";
        boolean bool = JDBCUtil.executeSQL(SQL,content.getLikeCount(),content.getId());
        return bool;
    }

    public List<Content> ten() {
        String SQL = "select id , " + column + " from" + TableNames.CONTENT_TABLE_NAME + " order by like_count desc limit ? ";
        ResultSet resultSet = JDBCUtil.selectSQL(SQL, 10);
        List<Content> warp = null;
        try {
            warp = warp(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return warp;
    }
}
